package com.garbuz.svnadmin.data
{
	import actionlib.common.query.conditions.nameIs;
	import actionlib.common.query.from;

	import com.garbuz.svnadmin.dto.RepoTO;

	import mx.collections.ArrayCollection;

	public class Repo
	{
		public static function fromTO(data:RepoTO):Repo
		{
			var repo:Repo = new Repo();
			repo.name = data.name;
			repo.date = data.date;
			repo.lastCommitDate = data.lastCommitDate;
			repo.lastCommitAuthor = data.lastCommitAuthor;
			var permissionsArray:Array = from(data.permissions).select(Permission.fromTO);
			permissionsArray.sortOn("sortOrder");
			repo.permissions = new ArrayCollection(permissionsArray);

			return repo;
		}
		
		[Bindable]
		public var name:String;

		[Bindable]
		public var date:Date;

		[Bindable]
		public var lastCommitDate:Date;

		[Bindable]
		public var lastCommitAuthor:String;

		[Bindable]
		public var permissions:ArrayCollection;

		public function hasPermission(permissionName:String):Boolean
		{
			return from(permissions).where(nameIs(permissionName)).exists();
		}

		public function getPermission(permissionName:String):Permission
		{
			return from(permissions).where(nameIs(permissionName)).findFirst()
		}
	}
}